package com.think.teaching.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.common.utils.Common;
import com.think.teaching.mapper.CourseMemberMapper;
import com.think.teaching.model.CourseMember;

@Service
public class CourseMemberServiceImpl extends BaseServiceImpl<CourseMemberMapper, CourseMember> {
	
	public CourseMember GetCourseMemberById(Integer id) {
		return this.getById(id);
	}
	
	public CourseMember AddCourseMember(CourseMember data) {
		data.setCreatedBy(Common.GetCurrentAuthor().getUserId());
		return this.save(data)?data:null;
	}
	
	public IPage<CourseMember> ListCourseMembers(Integer page,Integer count,Integer courseId,Boolean withoutBan){
		IPage<CourseMember> iPage = new Page<CourseMember>(page,count);
		QueryWrapper<CourseMember> queryWrapper = new QueryWrapper<CourseMember>();
		queryWrapper.ne("delflag", 1).eq("course_id", courseId);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
	
		queryWrapper.orderByDesc("`order`");
		
		return this.page(iPage,queryWrapper);
	}
	
	public List<CourseMember> GetAllMembersByCourseId(Integer courseId){
		QueryWrapper<CourseMember> queryWrapper = new QueryWrapper<CourseMember>();
		queryWrapper.ne("delflag", 1).eq("course_id", courseId).eq("state", 1).orderByDesc("`order`");
		return this.list(queryWrapper);
	}
	
	public Boolean DeleteCourseMember(Integer id) {
		UpdateWrapper<CourseMember> updateWrapper = new UpdateWrapper<CourseMember>();
		updateWrapper.eq("id", id).set("delflag", 1);
		return this.update(updateWrapper);
	}
	
	public Boolean UpdateCourseMember(CourseMember data) {
		data.setUpdatedBy(Common.GetCurrentAuthor().getUserId());
		return this.updateById(data);
	}

}
